<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="add(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickShowEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="dle(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <el-dialog :visible="showDialog" :title="showTitle" @close="closeShowDialog">
        <!-- 表单内容 -->
        <el-form label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text=""
              inactive-text=""
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetPermissionList, reqDelPermission, reqAddPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      showTitle: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = tranListToTreeData(data, '0')
    },
    // X按钮
    closeShowDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 删除
    async dle(id) {
      const ress = await this.$confirm('确实删除吗?', '提示').catch(error => error)
      if (ress !== 'confirm') return
      const res = await reqDelPermission(id)
      this.$message.success(res.message)
      this.getPermissionList()
    },
    // 添加权限
    add(type, id) {
      this.showTitle = '添加权限'
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = id
    },
    // 添加/更新权限
    async clickSubmit() {
      if (this.formData._id) {
        const res = await reqUpdatePermission(this.formData)
        this.$message.success(res.message)
      } else {
        const res = await reqAddPermission(this.formData)
        this.$message.success(res.message)
      }
      this.getPermissionList()
      this.closeShowDialog()
    },
    // 查看回显数据
    async  clickShowEdit(id) {
      this.showDialog = true
      this.showTitle = '更新权限'
      const res = await reqGetPermissionDetail(id)
      this.formData = res.data
    }
  }
}
</script>

<style lang="scss">

</style>
